<script setup lang="ts">
import type { LoginDTO } from '@/types/login'
import request from '@/utils/request'
import type { FormInstance, FormRules } from 'element-plus'
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue' 
// 数据绑定对象
const loginDTO = reactive<LoginDTO>({
  username: 'sea',
  password: 'show1234',
})

// 加载动画
const loading = ref(false)

// 验证规则
const rules = reactive<FormRules>({
  username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
  password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
})

// 定义表单校验实例
const loginFormRef = ref<FormInstance>()

const handleLogin = async () => {
  // 1.加载 loading
  loading.value = true
  // 验证通过后，提交表单
  const valid = await loginFormRef.value?.validate()
  // 校验不成功直接 return
  if (!valid) {
    return false
  }

  // 请求后端接口
  request
    .post('/auth/login', loginDTO)
    .then(res => {
      console.log(res)
    })
    .catch(() => {
      // 验证失败，重新加载验证码
    })
    .finally(() => {
      // 验证结束，隐藏loading
      loading.value = false
    })
}

</script>

<template>
  <div class="navy-login-container">
    <div class="navy-form-wrapper">
      <!-- 品牌标识 -->
      <div class="brand-header">
        <img src="./assets/navy-logo.png" class="navy-logo" />
        <h1>海Ⅲ系统</h1>
      </div>

      <!-- 登录表单 -->
      <div class="form-content">
        <div class="form-header">
          <h2>登录</h2>
          <p>请输入用户名和密码</p>
        </div>
        
        <el-form ref="loginFormRef" :model="loginDTO" :rules="rules">
          <!-- 用户名输入 -->
          <el-form-item prop="username">
            <el-input 
              v-model="loginDTO.username"
              placeholder="请输入用户名"
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <!-- 密码输入 -->
          <el-form-item prop="password">
            <el-input
              v-model="loginDTO.password"
              type="password"
              placeholder="请输入密码"
              class="custom-input"
              show-password
            >
              <template #prefix>
                <el-icon class="input-icon"><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <!-- 登录按钮 -->
          <el-form-item>
            <el-button 
              type="primary"
              class="submit-btn"
              :loading="loading"
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
// 全屏军事风格容器

.navy-login-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: url('./image.png') no-repeat 100% center/cover; // 背景左移35%

  .navy-form-wrapper {
    position: absolute;
    right: 6.5%;
    top: 40%;
    transform: translateY(-50%);
    width: 300px;
    height: 300px;
    background: #fff;
    // border: 1px solid rgba(24, 144, 255, 0.08);
    border-radius: 20px;
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
    padding: 25px 25px;

    // 品牌标识
    .brand-header {
      display:flex;
      // align-items: center;
      justify-content: center;
      gap:12px;
      text-align:center ;
      margin-bottom: 5px;

      .navy-logo {
        width: 35px;
        height: 35px;
        margin-bottom: 12px;
      }

      h1 {
        font-weight: 700;
        font-size: 25px;
        color: #020202;
        letter-spacing: 1.2px;
      }
    }

    // 表单标题
    .form-header {
      margin-bottom: 12px;
      text-align: left;

      h2 {
        font-weight: 600;
        font-size: 20px;//登录字体大小
        color: #080808;
        margin-bottom: 8px;
      }

      p {
        font-size: 12px;
        color: #666;
      }
    }

    // 输入框样式
    .custom-input {
      :deep(.el-input__wrapper) {
        height: 32px;
        border-radius: 5px;
        border: 1px solid #dcdfe6;
        padding: 0 15px;
        padding-left: 8px;//输入框内内容向左
        &:hover {
          border-color: #1890ff;
        }

        &.is-focus {
          border-color: #1890ff;
          box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
        }

        .el-input__prefix {
          display: flex;
          align-items: center;
          padding-right: 0px;
        }
      }
    }

    // 登录按钮
    .submit-btn {
      width: 100%;
      height: 32px;
      font-size: 16px;
      color: #fff;
      background: #3446d2;
      border-radius: 20px;
      transition: all 0.3s;
      // margin-top: -2px; /* 关键调整参数 */
      // transform: translateY(-8%); /* 微调定位 */
      &:hover {
        background: #1475d4;
        transform: translateY(-1px);
      }
    }
  }
}
// 输入框图标样式
.input-icon {
  color: #909399;      // 图标颜色
  font-size: 13px;     // 图标大小
  margin-right: 10px;   // 图标与文字间距
}
</style>